package com.fch.server.service;

import com.fch.server.domain.CourseCategory;
import com.fch.server.domain.CourseCategoryExample;
import com.fch.server.dto.CategoryDto;
import com.fch.server.dto.CourseCategoryDto;
import com.fch.server.dto.PageDto;
import com.fch.server.mapper.CourseCategoryMapper;
import com.fch.server.util.CopyUtil;
import com.fch.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: fangchuhao
 **/
@Service
public class CourseCategoryService {
    private static final Logger LOG = LoggerFactory.getLogger(CourseCategoryService.class);

    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();
        List<CourseCategory> courseCategorys = courseCategoryMapper.selectByExample(courseCategoryExample);
        PageInfo<CourseCategory> courseCategoryPageInfo = new PageInfo<>(courseCategorys);
        pageDto.setTotal(courseCategoryPageInfo.getTotal());
        List<CourseCategoryDto> courseCategoryDtos = CopyUtil.copyList(courseCategorys, CourseCategoryDto.class);
        pageDto.setList(courseCategoryDtos);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(CourseCategoryDto courseCategoryDto) {
        CourseCategory courseCategory = CopyUtil.copy(courseCategoryDto,CourseCategory.class);
        if (StringUtils.isEmpty(courseCategory.getId())) {
            insert(courseCategory);
        }else {
            update(courseCategory);
        }
    }

    /**
     * 新增
     */
    private void insert(CourseCategory courseCategory) {
        courseCategory.setId(UuidUtil.getShortUuid());
        courseCategoryMapper.insert(courseCategory);
    }

    /**
     * 更新
     */
    private void update(CourseCategory courseCategory) {
        courseCategoryMapper.updateByPrimaryKey(courseCategory);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        courseCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据某一课程，先清空课程分类，再保存课程分类
     * @param courseId
     * @param categorys
     */
    @Transactional
    public void saveBatch(String courseId, List<CategoryDto> categorys) {
        CourseCategoryExample example = new CourseCategoryExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        courseCategoryMapper.deleteByExample(example);
        for (CategoryDto category : categorys) {
            CourseCategory courseCategory = new CourseCategory();
            courseCategory.setId(UuidUtil.getShortUuid());
            courseCategory.setCourseId(courseId);
            courseCategory.setCategoryId(category.getId());
            insert(courseCategory);
        }
    }

    /**
     * 查找课程下的所有分类
     * @param courseId
     */
    public List<CourseCategoryDto> listByCourse(String courseId) {
        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();
        courseCategoryExample.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseCategory> courseCategorys = courseCategoryMapper.selectByExample(courseCategoryExample);
        return CopyUtil.copyList(courseCategorys, CourseCategoryDto.class);
    }
}
